package com.sunye.upload.controller;

import cn.hutool.core.text.CharSequenceUtil;
import com.sunye.upload.plugin.FileManagerPlugin;
import com.sunye.upload.plugin.UploadImageService;
import com.sunye.utils.Base64DecodeMultipartFile;
import com.sunye.utils.FastDFSClientWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.crypto.Mac;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 文件上传接口
 *
 * @author Chopper
 * @since 2020/11/26 15:41
 */
@Slf4j
@RestController
@Api(tags = "文件上传接口")
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private FileManagerPlugin fileManagerPlugin;

    @Autowired
    private UploadImageService uploadImageService;

    @Autowired
    private FastDFSClientWrapper dfsClient;


    /**
     * 阿里云上设置数据处理
     * 上传视频在视频路径下加上?x-oss-process=video/snapshot,t_100,f_jpg 作为视频封面图
     * 上传图片路径下加?x-oss-process=style/400X400 图片缩率图
     * @param file
     * @param base64
     * @return
     *
     */
    @ApiOperation(value = "文件上传")
    @PostMapping(value = "/uploadByOss")
    public String uploadByOss(MultipartFile file,String base64) {
        if (file == null || CharSequenceUtil.isEmpty(file.getContentType())) {
            return "添加上传图片";
        }

//        if (!CharSequenceUtil.containsAny(file.getContentType().toLowerCase(), "image")) {
//            throw new ServiceException(ResultCode.FILE_TYPE_NOT_SUPPORT);
//        }

        if (CharSequenceUtil.isNotBlank(base64)) {
            //base64上传
            file = Base64DecodeMultipartFile.base64Convert(base64);
        }
        String result;
        try {
            InputStream inputStream = file.getInputStream();
            //上传至第三方云服务或服务器
            result = fileManagerPlugin.inputStreamUpload(file,inputStream);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "上传成功";
    }
    //删除oss
    @ApiOperation(value = "删除文件")
    @PostMapping(value = "/deleteByOss")
    public String deleteByOss(String fileKey) {
        if (StringUtils.isBlank(fileKey)) {
            return "上传图片路径";
        }
        List<String> list = new ArrayList<>();
        list.add(fileKey);
        fileManagerPlugin.deleteFile(list);
        return "删除成功";
    }

    /**
     * 上传fastDfs
     * 上传视频会有封面图
     * 上传图片会压缩
     */
    @PostMapping("/uploadByfastDfs")
    public Map uploadByfastDfs(@RequestParam("file") MultipartFile file) throws IOException {
        // 省略业务逻辑代码。。。
        Map imgUrl = dfsClient.uploadByfastDfs(file);
        return imgUrl;
    }

    /**
     * 上传fastDfs
     * 正常图片
     */
    @PostMapping("/uploadImageAndCrtThumbImage")
    public Map uploadImageAndCrtThumbImage(@RequestParam("file") MultipartFile file){
        // 省略业务逻辑代码。。。
        Map imgUrl = dfsClient.uploadImageAndCrtThumbImage(file);
        return imgUrl;
    }


    /**
     * 上传fastDfs 多个自定义缩略图
     * 正常图片
     */
    @PostMapping("/uploadOwnImageAndCrtThumbImage")
    public Map uploadOwnImageAndCrtThumbImage(@RequestParam("file") MultipartFile file){
        // 省略业务逻辑代码。。。
        Map imgUrl = dfsClient.uploadOwnImageAndCrtThumbImage(file);
        return imgUrl;
    }

    //删除 fileKey全路径
    @ApiOperation(value = "删除文件")
    @PostMapping(value = "/deleteByFastDfs")
    public String deleteByFastDfs(String fileKey) {
        if (StringUtils.isBlank(fileKey)) {
            return null;
        }
        dfsClient.deleteFile(fileKey);
        return "删除成功";
    }



    /**
     * 七牛云上设置图片样式
     * 上传视频在视频路径下加上?vframe/jpg/offset/7/w/480/h/360 得到视频第一帧图的地址，作为封面
     * 上传图片路径下加 -200X200（根据分隔符和样式名称来的） 图片缩率图
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/uploadByQiNiu")
    private String uploadByQiNiu(@RequestParam("file") MultipartFile file) throws IOException {
        // 获取文件的名称
//        String fileName = file.getOriginalFilename();
        // 使用工具类根据上传文件生成唯一图片名称
        String imgName = String.format("%s.%s", UUID.randomUUID().toString(), FilenameUtils.getExtension(file.getOriginalFilename()));
        if (!file.isEmpty()) {
            FileInputStream inputStream = (FileInputStream) file.getInputStream();
            String path = uploadImageService.uploadQNImg(inputStream, imgName);
            System.out.print("七牛云返回的图片链接:" + path);
            return path;
        }
        return "上传失败";
    }


    //删除
    @ApiOperation(value = "删除文件")
    @PostMapping(value = "/deleteByQiNiu")
    public Boolean deleteByQiNiu(String fileKey) {
        if (StringUtils.isBlank(fileKey)) {
            return null;
        }
        Boolean deleteBoolean = uploadImageService.deleteFile(fileKey);
        return deleteBoolean;
    }


}
